package com.yami.shop.rights.common.listener;

import com.yami.shop.bean.event.SubmitOrderQualificationEvent;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.rights.common.dao.PersonRightsMapper;
import com.yami.shop.rights.common.dto.ActivityPresellQualificationDto;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("SubmitOrderAddQualificationIdListener")
@AllArgsConstructor
public class SubmitOrderAddQualificationIdListener {

    @Autowired
    private PersonRightsMapper personRightsMapper;

    /**
     * 功能描述: 扣减购买次数
     * @Author: LSR
     * @Date: 2023/6/25 18:07
     */

    @EventListener(SubmitOrderQualificationEvent.class)
    public void rightsSubmitOrderListener(SubmitOrderQualificationEvent event) {
            for(OrderItem orderItem : event.getOrderItems()) {
                // 查询商品权益资格
                List<ActivityPresellQualificationDto> qualificationDtos = personRightsMapper.getPresellerQualification(orderItem.getProdId(),event.getOrderMold(), orderItem.getUserId());
                for (ActivityPresellQualificationDto qualificationDto : qualificationDtos) {
                    orderItem.setQualificationId(qualificationDto.getId());
                    // 扣除 优先购资格次数
                    personRightsMapper.verificationPresellerRights(qualificationDto.getId(), 1);
                    // 当前需求只消耗一次优先购购买资格
                    break;
                }
            }
    }
}
